package com.sanjiahantang.youjiaclient.umengshare;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import static com.sanjiahantang.youjiaclient.config.ConfigValue.SHARE_IP;

/**
 * Created by yan on 2017/12/13.
 */

public class UmengShareManager {


    /**
     * @param context
     * @param url
     */
    public static void share(final Activity context,String url) {
        share(context,"优家库客户端","优家库客户端详解",url);
    }
    public static void share(final Activity context,String title,String content, String url) {

//        if (Build.VERSION.SDK_INT >= 23) {
//            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION,
//                    Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE,
//                    Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP,
//                    Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
//            int permission = ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
//            if (permission != PackageManager.PERMISSION_GRANTED ||
//                    ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
//                ActivityCompat.requestPermissions(context, mPermissionList, 123);
//            } else {
//                UMWeb web = new UMWeb(SHARE_IP + url);
//        web.setTitle("优家库客户端");//标题
//        web.setThumb(new UMImage(context, R.mipmap.ic_launcher));  //缩略图
//        web.setDescription("优家库客户端详解");
//        new ShareAction(context).withMedia(web)
//                .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN_FAVORITE)
//                .setCallback(new ShareListener(context))
//                .open();
//            }
//        } else {

        UMWeb web = new UMWeb(SHARE_IP + url);
        web.setTitle(title);//标题
        web.setThumb(new UMImage(context, R.mipmap.ic_launcher));  //缩略图
        web.setDescription(content);
        new ShareAction(context).withMedia(web)
                .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN_FAVORITE)
                .setCallback(new ShareListener(context))
                .open();

//        }
    }

    /**
     * 共享监听类
     */
    private static class ShareListener implements UMShareListener {

        private Activity context;

        public ShareListener(Activity context) {
            this.context = context;
        }

        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            Utils.showToast(context, "分享成功");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            Utils.showToast(context, "分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            Utils.showToast(context, "取消分享");
        }
    }

}
